package com.youxin.dataSet;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.java.ExecutionEnvironment;
import org.apache.flink.api.java.operators.AggregateOperator;
import org.apache.flink.api.java.operators.DataSource;
import org.apache.flink.api.java.operators.FlatMapOperator;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.util.Collector;

public class DataSetJob {
    public static void main(String[] args) throws Exception {
        ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
        DataSource<String> lines = env.readTextFile("C:\\data\\input\\words", "UTF-8");
        FlatMapOperator<String, Tuple2<String, Long>> wordAndOne = lines.flatMap(new FlatMapFunction<String, Tuple2<String, Long>>() {
            @Override
            public void flatMap(String s, Collector<Tuple2<String, Long>> collector) throws Exception {
                String[] split = s.split(" ");
                for (String word : split) {
                    collector.collect(new Tuple2<>(word, 1L));
                }
            }
        });
        AggregateOperator<Tuple2<String, Long>> sum = wordAndOne.groupBy(0).sum(1);
        try {
            sum.print();
        } catch (Exception e) {
            e.printStackTrace();
        }
        env.execute("streaming job");

    }
}
